package com.yzy.fly.entity.role;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.yzy.fly.kernel.model.ability.tree.Tree;
import com.yzy.fly.type.ViewWidgetType;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Data
@TableName(value ="fly_view_widget",autoResultMap = true)
public class FlyViewWidget implements Serializable,Tree<String,FlyViewWidget> {

    @TableId(type = IdType.INPUT)
    private String id;
    private Integer seq;
    private String name;
    @TableField("`key`")
    private Integer key;
    private String category;
    private String type;
    private String icon;
    private boolean formItemFlag;
    private boolean internal;
    private boolean merged;
    @TableField(value = "options",typeHandler = FastjsonTypeHandler.class)
    private JSONObject options;

    @TableField(exist = false)
    private List<FlyViewWidget> tabs;
    @TableField(exist = false)
    private List<FlyViewWidget> widgetList;

    @TableField(exist = false)
    private List<FlyViewWidget> rows;
    @TableField(exist = false)
    private List<FlyViewWidget> cols;

    private String parentId;

    private Integer viewId;

    private Integer systemId;
    private Date createTime;

    private Date updateTime;

    @JSONField(serialize = false,deserialize = false)
    public List<FlyViewWidget> getChildrens(){
        List<FlyViewWidget> flyViewWidgets = null;
        ViewWidgetType viewWidgetType = ViewWidgetType.type(type);
        if (ViewWidgetType.TAB == viewWidgetType) {
            //tabs
            flyViewWidgets = getTabs();
        } else if (ViewWidgetType.TABLE == viewWidgetType) {
            // rows -> cols
            flyViewWidgets = getRows();
        } else if (ViewWidgetType.CARD == viewWidgetType || ViewWidgetType.TABLE_CELL == viewWidgetType) {
            //widgetList
            flyViewWidgets = getWidgetList();
        } else if (ViewWidgetType.GRID == viewWidgetType) {
            //cols
            flyViewWidgets = getCols();
        }
        return flyViewWidgets;
    }

    public void setChildrens(List<FlyViewWidget> childrens){
        if (CollectionUtils.isEmpty(childrens)) {
            return;
        }
        ViewWidgetType viewWidgetType = ViewWidgetType.type(type);
        if (ViewWidgetType.TAB == viewWidgetType) {
            //tabs
            setTabs(childrens);
        } else if (ViewWidgetType.TABLE == viewWidgetType) {
            // rows -> cols
            setRows(childrens);
        } else if (ViewWidgetType.CARD == viewWidgetType || ViewWidgetType.TABLE_CELL == viewWidgetType) {
            //widgetList
            setWidgetList(childrens);
        } else if (ViewWidgetType.GRID == viewWidgetType) {
            //cols
            setCols(childrens);
        }
    }

    @JSONField(serialize = false,deserialize = false)
    @Override
    public boolean isRootNode() {
        if (StringUtils.isBlank(parentId)) {
            return true;
        }
        return false;
    }
    @JSONField(serialize = false,deserialize = false)
    @Override
    public boolean isParentNode(String parentId) {
        if (StringUtils.isNotBlank(id) && StringUtils.isNotBlank(parentId)) {
            if (id.equals(parentId)) {
                return true;
            }
        }
        return false;
    }
    @JSONField(serialize = false,deserialize = false)
    @Override
    public boolean isChildNode(FlyViewWidget parent) {
        if(parentId.equals(parent.getId())) {
            return true;
        }
        return false;
    }
}
